from assignment_3.src.scripts import getitem, full_connection, _getitem
import pandas as pd

if __name__ == '__main__':
    data_set = [{'A', 'C', 'D'}, {'B', 'C', 'E'}, {'A', 'B', 'C', 'E'}, {'B', 'E'}]  # 数据集
    item_set = []
    _item_set = []
    frequent_itemsets = []
    _frequent_itemsets = {'support': [], 'set': []}

    min_support = 0.5  # 设定最小支持度

    while len(_item_set) > 1 or len(item_set) == 0:
        if len(item_set) == 0:
            item_set = getitem(data_set, min_support)  # 渠道项集并且排除支持度小于最小支持度的项
        else:
            item_set = _getitem(_item_set, data_set, min_support)

        frequent_itemsets += item_set
        _item_set = full_connection(item_set)  # 对k项进行全连接形成k+1项

    # 构建DataFrame
    for i in frequent_itemsets:
        count = 0
        for j in data_set:
            if i.issubset(j):
                count += 1

        support = count / len(data_set)
        _frequent_itemsets['support'].append(support)
        _frequent_itemsets['set'].append(i)

    print(pd.DataFrame(_frequent_itemsets))

# from mlxtend.preprocessing import TransactionEncoder
# from mlxtend.frequent_patterns import apriori
#
# # 数据集
# dataset = [['A', 'C', 'D'], ['B', 'C', 'E'], ['A', 'B', 'C', 'E'], ['B', 'E']]
#
# # 将数据集转换为one-hot编码形式
# te = TransactionEncoder()
# te_ary = te.fit(dataset).transform(dataset)
# df = pd.DataFrame(te_ary, columns=te.columns_)
#
# # 使用apriori算法挖掘频繁项集
# frequent_itemsets = apriori(df, min_support=0.5, use_colnames=True)
#
# # 打印频繁项集
# print(frequent_itemsets)
